<template>
    <view class="content">

		<view class="cu-form-group between">
		    <view class="title">银行名称</view>
			<picker @change="returnPickerBankList" range-key="name" :range="banklist">
			    <view class="picker">
			        {{banklist[form.bankName]?banklist[form.bankName].name:'请选择银行名称'}}
			    </view>
			</picker>
		    <!-- <input v-model="form.bankName" placeholder="请输入银行名称"></input> -->
		</view>

        <view class="cu-form-group between">
            <view class="title">放款金额</view>
            <input v-model="form.amount" placeholder="请输入" type="number"></input>
        </view>

        <view class="cu-form-group" @click="showFn(1)">
            <view class="title">放款日期</view>
            <view :class="form.releaseDate?'':'c9'">{{timeF(form.releaseDate)||'请选择'}}</view>
            <u-datetime-picker :defaultIndex="[1,0,0]" :minDate="minDate" :maxDate="maxDate" :show="show1"
                @cancel="show1 = !!0" @confirm="show1= !!0" v-model="form.releaseDate" mode="date"></u-datetime-picker>
        </view>
        <view class="cu-form-group" style="justify-content: flex-start;">
            <view class="title">还款方式</view>
            <view style="width: 240rpx;">
                <picker @change="returnPickerType" :value="form.repayMethod" range-key="name" :range="types">
                    <view class="picker">
                        {{types[form.repayMethod]?types[form.repayMethod].name:'请选择还款方式'}}
                    </view>
                </picker>
            </view>

        </view>

        <view class="cu-form-group between">
            <view class="title">还款金额</view>
            <input v-model="form.repayAmount" placeholder="请输入" type="number"  step="0.01"></input>
        </view>

        <view class="cu-form-group" style="justify-content: flex-start; align-items: center;">
            <view class="title">年化利率</view>
            <view style="width: 200rpx;">
                <input v-model="form.rate" placeholder="请输入" type="digit"></input>
            </view>%
        </view>

        <view class="cu-form-group">
            <view class="title">每月还款日</view>
            <input v-model="form.releaseDay" placeholder="请输入" type="number" ></input>
        </view>

        <view class="cu-form-group between">
            <view class="title">每月还款金额</view>
            <input v-model="form.repayMonth" placeholder="请输入" type="number"  step="0.01"></input>
        </view>

        <view class="cu-form-group" @click="showFn(2)">
            <view class="title">初次还款日</view>

            <view :class="form.releaseBeginDate?'':'c9'">{{timeF(form.releaseBeginDate)||'请选择'}}</view>
            <u-datetime-picker :defaultIndex="[1,0,0]" :minDate="minDate" :maxDate="maxDate" :show="show2"
                @cancel="show2 = !!0" @confirm="show2= !!0" v-model="form.releaseBeginDate"
                mode="date"></u-datetime-picker>
        </view>
        <view class="cu-form-group" @click="showFn(3)">
            <view class="title">末次还款日</view>

            <view :class="form.releaseEndDate?'':'c9'">{{timeF(form.releaseEndDate)||'请选择'}}</view>
            <u-datetime-picker :defaultIndex="defaultIndex" :minDate="minDate" :maxDate="maxDate" :show="show3"
                @cancel="show3 = !!0" @confirm="show3= !!0" v-model="form.releaseEndDate"
                mode="date"></u-datetime-picker>

        </view>


        <view class="cu-form-group" style="justify-content: flex-start; align-items: center;">
            <view class="title">授信期限</view>
            <view style="width: 200rpx;">

                <input v-model="form.creditTerm" placeholder="请输入" type="number">
            </view></input>月
        </view>

        <view class="btn-box" v-if="isLook!=1">
            <view class="submit-btn" @click="submit">保存</view>
        </view>

        <view v-if="isLook" class="mask"></view>
    </view>
</template>

<script>
    import api from '@/api/api.js';
    export default {
        data() {
            return {
                defaultIndex: [1, 0, 0],
                item: '',
                show1: !!0,
                show2: !!0,
                show3: !!0,
                maxDate: '',
                minDate: '',
                isLook: 0,
                types: [
                    { id: 1, name: '先息后本' },
                    { id: 2, name: '等额本息' },
                ],
				banklist: [
				    { id: 1, name: '中国银行' },
				    { id: 2, name: '农业银行' },
				    { id: 3, name: '建设银行' },
				    { id: 4, name: '工商银行' },
				    { id: 5, name: '郑州银行' },
				    { id: 6, name: '民生银行' },
            { id: 7, name: '农商银行' },
            { id: 8, name: '中原银行' },
            { id: 9, name: '邮政银行' },
            { id: 10, name: '临颍中银富登银行' },
            { id: 11, name: '舞阳玉川银行' },
            { id: 12, name: '郾城中原村镇银行' },
            { id: 13, name: '广发银行' },
            { id: 14, name: '浦发银行' },
            { id: 15, name: '招商银行' },
            { id: 16, name: '中信银行' },
            { id: 17, name: '平安银行' },
          ],
                form: {
                    bankName: '',
                    amount: '',
                    releaseDate: '',
                    releaseDay: '',
                    releaseBeginDate: '',
                    releaseEndDate: '',
                    creditTerm: '',
                    status: 1, //1已执行 0未执行
                    repayMethod: '', //0 先息后本 1等额本息
                    repayAmount: '', //还款金额
                    rate: '', //利率
                    repayMonth: '', //每月还款金额
                }
            }
        },
        onLoad(o) {
			this.banklist = JSON.parse(uni.getStorageSync('dicts')).bankName;
            let currentDate = new Date();
            let currentYear = currentDate.getFullYear();
            let lastYear = currentYear - 1;
            let m = currentDate.getMonth()
            let d = currentDate.getDate()
            if (m == 1 && d == 29) {
                d = 28
                this.defaultIndex = [1, 0, 1]
            }
            let targetDate = new Date(lastYear, m, d);

            console.log(lastYear, currentDate.getMonth(), currentDate.getDay()); // 获取对应的时间戳（以毫秒为单位）
            const timestamp = targetDate.getTime();
            this.minDate = timestamp
            this.maxDate = +new Date('2224-12-30')
            this.item = this.getS(`activeCustomerDetail`)
            this.form = {
                customerId: this.item.customerId + '',
                refuseMsg: '',
                impStatus: '1',
                impId: this.item.impId + '',
                bankName: '',
                amount: '',
                releaseDate: '',
                releaseDay: '',
                releaseBeginDate: '',
                releaseEndDate: '',
                status: 1, //1已执行 0未执行
                repayMethod: '', //0 先息后本 1等额本息
                repayAmount: '', //还款金额
                rate: '', //利率
                repayMonth: '', //每月还款金额
            }
            if (o.type == 'look') {
                this.isLook = 1

                api.getOneImp({
                    customerId: this.item.customerId
                }).then(r => {
                    r = r.data
                    let res = r
                    console.log('执行信息', r);
                    this.form = {
                        customerId: this.item.customerId + '',
                        refuseMsg: '',
                        impStatus: '1',
                        impId: this.item.impId + '',
                        bankName: r.bankName,
                        amount: r.amount,
                        releaseDate: r.releaseDate,
                        releaseDay: r.releaseDay,
                        releaseBeginDate: r.releaseBeginDate,
                        releaseEndDate: r.releaseEndDate,
                    }
                })
            }
            console.log(1234, this.form);

        },
        methods: {
            showFn(i) {
                // if (i == 1) this.form.releaseDate = Date.now()
                // if (i == 2) this.form.releaseBeginDate = Date.now()
                // if (i == 3) this.form.releaseEndDate = Date.now()
                this[`show${i}`] = !!1
            },

            timeF(time) {
                if (time) {
                    return uni.$u.timeFormat(time, 'yyyy-mm-dd')
                } else {
                    return 0
                }
            },
            returnPickerType(e) {
                this.form.repayMethod = e.detail.value
            },
            returnPickerBankList(e) {
              this.form.bankName = e.detail.value
            },
            valireleaseDatePhoneNumber(phoneNumber) {
                let reg = /^(13[0-9]|14[5-9]|15[0-1,5-9]|16[2,5-6]|17[1-8]|18[0-9]|19[1-9])\d{8}$/;

                if (reg.test(phoneNumber)) {
                    return true;
                } else {
                    return false;
                }
            },
            submit() {
                if (!this.form.bankName) {
                    uni.showToast({
                        title: "请输入银行",
                        icon: 'none',
                    });
                    return
                }

                if (this.form.amount - 0 <= 0) {
                    uni.showToast({
                        title: "请输入正确的放款金额",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.releaseDate) {
                    uni.showToast({
                        title: "请输入放款日期",
                        icon: 'none',
                    });
                    return
                }
                if ('' === this.form.repayMethod) {
                    uni.showToast({
                        title: "请选择还款方式",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.repayAmount) {
                    uni.showToast({
                        title: "请输入还款金额",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.rate) {
                    uni.showToast({
                        title: "请输入还款利率",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.releaseDay) {
                    uni.showToast({
                        title: "请输入每月还款日",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.repayMonth) {
                    uni.showToast({
                        title: "请输入每月还款金额",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.releaseBeginDate) {
                    uni.showToast({
                        title: "请输入初次还款日",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.releaseEndDate) {
                    uni.showToast({
                        title: "请输入末次还款日",
                        icon: 'none',
                    });
                    return
                }
                if (!this.form.creditTerm) {
                    uni.showToast({
                        title: "请输入授信期限",
                        icon: 'none',
                    });
                    return
                }
                api.impEdit(this.form).then(
                    res => {
                        if (res.code == 200) {
                            uni.showToast({
                                title: '提交成功',
                                duration: 2000
                            });
                            setTimeout(function() {
                                uni.navigateBack({
                                    success: () => {}
                                })
                            }, 500);
                        } else {
                            uni.showToast({
                                title: res.msg,
                                icon: 'none',
                            });
                        }

                    },
                    rej => {}
                );

                if (this.form.id) { //编辑
                    data.customerId = data.id
                    api.editCustomer(data).then(
                        res => {
                            if (res.code == 200) {
                                uni.showToast({
                                    title: '操作成功',
                                    duration: 2000
                                });

                                setTimeout(function() {
                                    uni.navigateBack({
                                        success: () => {}
                                    })
                                }, 500);
                            } else {
                                uni.showToast({
                                    title: res.msg,
                                    icon: 'none',
                                });
                            }

                        },
                        rej => {}
                    );
                    return
                }

            }
            // 客户来源
            // sourcePicker(e) {
            // 	this.index = e.detail.value
            // },
        }
    }
</script>

<style scoped lang="scss">
    .content {
        margin-top: 20upx;

        .floow-cont {
            flex-direction: column;
            background-color: #ffffff;
            padding: 1rpx 30rpx;
            margin-top: 20upx;

            .title {
                text-align: justify;
                padding-right: 30upx;
                font-size: 30upx;
                position: relative;
                height: 60upx;
                line-height: 60upx;
            }

            textarea {
                width: 100%;
                height: 200upx;
                padding: 20upx 0;
            }
        }
    }

    .btn-box {
        padding: 20upx 24upx;

        .submit-btn {
            height: 80upx;
            line-height: 80upx;
            text-align: center;
            font-size: 28upx;
            color: #fff;
            border-radius: 8upx;
            background: #39b54a;
        }
    }

    .title {
        width: 7em;
        text-align: right;
    }

    .mask {
        position: fixed;
        z-index: 99999;
        left: 0;
        top: 44px;
        right: 0;
        bottom: 0;
    }
</style>